//
//  RHShowProductView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/8/18.
//

#import "RHShowProductView.h"
#import "RHProductCell.h"
#import "RHBrowsePictureView.h"

@interface RHShowProductView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;


@end
@implementation RHShowProductView
-(instancetype)init{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    UIView *grayView = [[UIView alloc]init];
    grayView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
    [self addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(dismiss)];
    [grayView addGestureRecognizer:tap];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat itemWidth = (kUI_WINDOW_W - 3*20)/2;
    layout.minimumLineSpacing = 20;
    layout.minimumInteritemSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20,  20);
    layout.itemSize = CGSizeMake(itemWidth, itemWidth*0.8);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.layer.cornerRadius = 8;
    _collectionView.layer.maskedCorners = kCALayerMinXMinYCorner|kCALayerMinXMaxYCorner;
    [self addSubview:_collectionView];
    [_collectionView registerClass:[RHProductCell class] forCellWithReuseIdentifier:@"RHProductCell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    kWeakSelf;
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_offset(0);
            make.bottom.mas_offset(kSafe_bottom);
            make.height.equalTo(weakSelf).multipliedBy(0.6);
    }];
    
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RHProductCell" forIndexPath:indexPath];
    cell.titleLab.text = [NSString stringWithFormat:@"%@ %ld",@"产品类型".rh_localized,indexPath.row+1];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RHProductCell *cell = (RHProductCell*)[collectionView cellForItemAtIndexPath:indexPath];
    CGRect fromRect = [cell convertRect:cell.productImg.frame toView:[UIApplication sharedApplication].keyWindow];
    RHBrowsePictureView *browseView = [[RHBrowsePictureView alloc]init];
    [browseView browsePictureFromRect:fromRect imageView:cell.productImg.image];
}
-(void)show
{
    self.frame = CGRectMake(0, 0, kUI_WINDOW_W, kUI_WINDOW_H);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
-(void)dismiss{
    [self removeFromSuperview];
}
@end
